package com.personal.gateway.web;

import com.personal.gateway.constant.LoginUserInfoConstant;
import com.personal.gateway.entity.FlinkCommandHistory;
import com.personal.gateway.entity.FlinkGatewayRoute;
import com.personal.gateway.service.FlinkConfigService;
import lombok.AllArgsConstructor;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * @program: FlinkGateway
 * @description: 配置
 * @author: yangliao
 * @create: 2021-08-23
 */
@RestController
@AllArgsConstructor
@RequestMapping("/flink_config")
public class FlinkConfigController {

    private final FlinkConfigService flinkConfigService;

    private final LoginUserInfoConstant loginUserInfoConstant;

    @PostMapping("/edit")
    public String edit(ServerHttpRequest request, @RequestBody FlinkGatewayRoute route, @CookieValue("token") String token) throws ExecutionException, InterruptedException {
        route.setUserName(loginUserInfoConstant.getUserByToken(token));
        return flinkConfigService.edit(route);
    }

    @PostMapping("/alarm_edit")
    public String alarmEdit(ServerHttpRequest request, @RequestBody FlinkGatewayRoute route, @CookieValue("token") String token) throws ExecutionException, InterruptedException {
        route.setUserName(loginUserInfoConstant.getUserByToken(token));
        return flinkConfigService.alarmEdit(route);
    }

    @GetMapping("/getProjectById")
    public FlinkGatewayRoute getProjectById(ServerHttpRequest request, Integer id) {
        return flinkConfigService.getProjectById(id);
    }

    @GetMapping("/del")
    public String del(ServerHttpRequest request, Integer id, @CookieValue("token") String token) {
        String user = loginUserInfoConstant.getUserByToken(token);
        return flinkConfigService.del(id, user);
    }

    @GetMapping("/route/view")
    public String routeView(ServerHttpRequest request) throws ExecutionException, InterruptedException {
        return flinkConfigService.routeView();
    }

    @GetMapping("/history")
    public List<FlinkCommandHistory> history(ServerHttpRequest request, Integer id) throws ExecutionException, InterruptedException {
        return flinkConfigService.history(id);
    }

    @GetMapping("/getHistoryById")
    public FlinkCommandHistory getHistoryById(ServerHttpRequest request, Integer id) throws ExecutionException, InterruptedException {
        return flinkConfigService.getHistoryById(id);
    }

}
